package com.aye.haze_detection;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Day_Adapter extends RecyclerView.Adapter<Day_Adapter.Day_Holder>{
    private static final String TAG = "Day_Adapter";
    private Context mContext;
    private List<Integer> data = new ArrayList<>();
    private int minValue;
    private int maxValue;

    public Day_Adapter(Context context, List<Integer> data){
        this.data.addAll(data);
        //灏嗕笉瑙勫垯鐨勬暟鎹粍浠庡皬鍒板ぇ鎺掑簭
        Collections.sort(this.data);
        minValue = this.data.get(0);
        maxValue = this.data.get(data.size()-1);
        mContext = context;
    }

    @NonNull
    @Override
    public Day_Holder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item, viewGroup,false);
        return new Day_Holder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull Day_Holder day_holder, int i) {


        if(i==0){
            day_holder.mTemperatureView.setDrawLeftLine(false);
        }
        //闄ょ涓�涓互澶�
        else{
            day_holder.mTemperatureView.setDrawLeftLine(true);
            day_holder.mTemperatureView.setLastValue(data.get(i-1));
        }

        //濡傛灉鏄渶鍚庝竴涓�
        if(i == data.size()-1){
            day_holder.mTemperatureView.setDrawRightLine(false);
        }
        //闄ゆ渶鍚庝竴涓互澶�
        else{
            day_holder.mTemperatureView.setDrawRightLine(true);
            day_holder.mTemperatureView.setNextValue(data.get(i+1));
        }

        day_holder.mTemperatureView.setCurrentValue(data.get(i));

    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    class Day_Holder extends RecyclerView.ViewHolder{

        private TemperatureView mTemperatureView;

        public Day_Holder(@NonNull View itemView) {
            super(itemView);
            mTemperatureView = itemView.findViewById(R.id.temp_view);
            mTemperatureView.setMinValue(minValue);
            mTemperatureView.setMaxValue(maxValue);
        }
    }
}
